<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../frame/layui/css/layui.css">
    <link rel="stylesheet" href="../frame/static/css/style.css">
    <link rel="icon" href="../frame/static/image/code.png">
</head>
<body class="body">


<div class="layui-form-item">
    <label class="layui-form-label">渠道名称:</label>

    <div class="layui-input-inline">
        <input type="text" id='cname' autocomplete="off" placeholder="请输入名称"
               class="layui-input">
    </div>
</div>
<div class="layui-form-item">
    <label class="layui-form-label" id="rnum">注册人数:</label>

    <div class="layui-input-inline">
        <input type="text" id='cnumber' lay-verify="required"
               class="layui-input">
    </div>
</div>

<div class="layui-layer-btn layui-layer-btn-c">
    <button class="layui-btn layui-btn-normal layui-btn-radius" onclick="updateorCreateUser()">提交</button>
</div>


<script src="../frame/layui/layui.js" charset="utf-8"></script>
<script src="/lib/jquery-2.1.4.js" charset="utf-8"></script>
<script type="text/javascript">
    layui.use(['form', 'layedit', 'laydate', 'element'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate
            , element = layui.element
            , $ = layui.jQuery;
    });
    var pkguid = GetQueryString("pkguid");
    console.log(GetQueryString("pkguid"))
    if (!!GetQueryString("pkguid")) {
        $.ajax({
            url: "/admin/selectChnnel?pkguid=" + GetQueryString("pkguid"),
            type: "get",
            success: function (result) {
                console.log(result)
                if (result.code === "200") {
                    $("#cname").val(result.data.cname);
                    $("#cnumber").val(result.data.cnumber);

                } else {
                    alert("操作失败");

                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest.responseText);

            }
        });

    }

    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    function updateorCreateUser() {
        var url;
        if (!!pkguid) {
            url = "/admin/updateChannel"
            var params = {
                "cname": $("#cname").val(),
                "cnumber": $("#cnumber").val(),
                "pkguid": pkguid
            }
        } else {
            url = "/admin/createChannel"
            var params = {
                "cname": $("#cname").val(),
                "cnumber": $("#cnumber").val(),
            }
        }

        $.ajax({
            url: url,
            type: "post",
            contentType: "application/json",
            data: JSON.stringify(params),
            success: function (result) {
                console.log(result)
                if (result.code === "200") {
                    alert("删除成功");
                    setTimeout(function () {
                        layer.closeAll('templates.index');
                    }, 3000);
                    parent.location.reload();

                } else {
                    alert("操作失败");
                }
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                console.log(XMLHttpRequest.responseText);

            }
        });
    }

</script>
</body>
</html>